package edu.ccut.saturn.studio.logic.model.drop;

import java.util.zip.ZipFile;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.internal.core.JarEntryFile;
import org.eclipse.jdt.internal.core.JarPackageFragmentRoot;
import org.w3c.dom.Element;

import edu.ccut.saturn.studio.logic.model.Shape;
import edu.ccut.saturn.studio.logic.model.saturn.SaturnViewNode;

/**
 * 拖拽Jsp文件时的处理者. 注意:不能区分scr文件加.
 * TODO: Alf ResourcesPlugin.getWorkspace().getRoot()
 * 
 * @author Alf
 * 
 */
public class SaturnViewerDrop extends AbstarctDropHandler {

	public static final String FILE_POSTFIX = ".jsp";

	public Shape getShape(String filePath ,IProject project) {

		String ss = filePath;
		String projectFilePath = "";

		String workspaceString = ResourcesPlugin.getWorkspace().getRoot()
				.getLocation().toOSString();
		String si = ss.replace(workspaceString + "\\", "");
		String[] s = si.split("\\\\");

		if (ResourcesPlugin.getWorkspace().getRoot().getProject(s[0]) == null) {
			System.out.println("Oh, Error");
			return null;
		}

		if (s[1].equalsIgnoreCase("src")) {
			for (int i = 1; i < s.length; i++) {
				if (i != 1)
					projectFilePath += s[i] + "/";
			}
		} else {
			for (int i = 1; i < s.length; i++) {
				projectFilePath += s[i] + "/";
			}
		}
		// for (int i = 1; i < s.length; i++) {
		// if(ResourcesPlugin.getWorkspace().getRoot().getProject(s[0]).getFolder(s[1]).getType()
		// == 2){
		//				
		// }
		// projectFilePath += s[i]+"\\";
		// // iFolder = iFolder.getFolder(s[i]);
		// }
		int i = projectFilePath.lastIndexOf("/");
		if (i > 0) {
			projectFilePath = projectFilePath.substring(0, i);
		}
		projectFilePath.trim();
		String symbolicName = super.getBundleName(project);
		SaturnViewNode viewerNode = new SaturnViewNode();
		viewerNode.setName(getComponentName(s[s.length - 1]));
		viewerNode.setType("jsp");
		viewerNode.setActionlocation("/"+symbolicName + "/"+projectFilePath);
		return viewerNode;
	}

	@SuppressWarnings("restriction")
	public Shape getShape(JarEntryFile jarEntryFile) throws Exception {
		String bundleName = super.getBundleNameFromJar(jarEntryFile);
		jarEntryFile.getFullPath();
		String pathName = jarEntryFile.getFullPath().toOSString();
		int i = pathName.lastIndexOf("\\");
		String fileName = pathName.substring(i+1, pathName.length());
		JarPackageFragmentRoot jar = (JarPackageFragmentRoot) jarEntryFile
				.getPackageFragmentRoot();
		ZipFile zipFile = null;
		try {
			zipFile = jar.getJar();
		} catch (CoreException e) {
			e.printStackTrace();
		}
		zipFile.getName();
		
		SaturnViewNode viewerNode = new SaturnViewNode();
		viewerNode.setName(fileName);
		viewerNode.setType("jsp");
		viewerNode.setActionlocation("/"+bundleName + jarEntryFile.getFullPath());

		return viewerNode;
	}

	private Shape getShape(Element root, String symbolicName) {
		return null;
	}

	/**
	 * 输入XXX.Jsp，返回XXX。
	 * 
	 * @param name
	 */
	private String getComponentName(String name) {
		int i = name.lastIndexOf(".");
		if (i > 0) {
			return name.substring(0, i).trim();
		}
		return "";
	}
}
